﻿function login() {
    $(".btn").focus().attr("disabled", "disabled");
    $.post("/system/login", $('form').serialize(), function (data) {
        $(".btn").blur().removeAttr("disabled");
        if (data.state) {
            location.href = data.value;
        } else {
            $('#ValidCode').next().attr('src', '/system/validcode?' + Math.random());
            $(".message").fadeIn().html(data.value);
        }
    });
}

function registy() {
    $('form').validBox(function (r) {
        if (r == false) {
            return;
        }

        $(".btn").focus().attr("disabled", "disabled");
        $.post("/system/registy", $('form').serialize(), function (data) {
            $(".btn").blur().removeAttr("disabled");
            if (data.state) {
                location.href = data.value;
            } else {
                $('#ValidCode').next().attr('src', '/system/validcode?' + Math.random());
                $(".message").fadeIn().html(data.value);;
            }
        });
    });
}

$(function () {
    $("form input").focus(function () {
        $(".message").hide();
    }).first().focus();

    $(document).keydown(function (e) {
        if (e.which == 13) login();
    });
});

function sendValidCode(dom) {
    var btn = $(dom);
    var tel = $("#TelPhone");
    tel.validBox(function (r) {
        if (r) {
            $.post("/system/sendValidCode", { telPhone: tel.val() });
            btn.attr("disabled", "disabled");
            var count = 30;
            var timer = setInterval(function () {
                btn.html("重新发送(" + count + ")");
                if (count-- == 0) {
                    clearInterval(timer);
                    btn.removeAttr("disabled").html("发送验证码");
                }
            }, 1000);
        }
    });
}